
import 'package:flutter/material.dart';
import 'package:xsdk_flutter/xsdk/framework/base/xsdk_function.dart';
import 'package:xsdk_flutter/xsdk/framework/component/select_time/bean/x_bean_select_time.dart';
import 'package:xsdk_flutter/xsdk/framework/component/select_time/widget/x_widget_select_time.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_date.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_flutter.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_image.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_number.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_string.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_style.dart';
import 'package:xsdk_flutter/xsdk/utils/x_utils_widget.dart';

/*
自定义选择时间控件
 */
abstract class XSelectTimeUtils{


  //1.时间选择widget(通用)
  static Widget getSelectTimeWidget(
      {
        required BuildContext context,
        required XBeanSelectTime? selectTime,
        required XFunctionResultCallback<dynamic> selectTimeResultCallback,
        bool isLimitSelectTimeOver=false,
        bool isClickMenu2AutoSubmit=true,
        XFunctionWidgetOnClick<XBeanSelectTime>? onClickOfLeftButton,
        XFunctionWidgetOnClick<XBeanSelectTime>? onClickOfRightButton,
        bool isShowLeftRightChangeButton=true,//是否显示左右切换按钮
        bool isBigText=true,
        Color textColor=const Color(0xFF999999),
        Color backgroundColor=Colors.white,
      }
      )
  {

    String showTimeStr=showSelectTimeString(selectTime:selectTime);
    if(XStringUtils.isEmpty(showTimeStr)){
      showTimeStr='选择日期';
    }

    double totalHeight=40;
    double textSize=isBigText?16:14;

    List<Widget> listWidgets=[];
    Widget selectDateWidget=XWidgetUtils.getWidgetGestureDetector(
        child: XWidgetUtils.getLayoutContainer(
          backgroundColor: backgroundColor,
            height: totalHeight,
            alignment: Alignment.center,
            child: XWidgetUtils.getLayoutRow(
                mainAxisSize:MainAxisSize.min,
                mainAxisAlignment:MainAxisAlignment.center,
                children: [
                  XWidgetUtils.getWidgetText(
                    showTimeStr,
                    padding: XWidgetUtils.getEdgeInsets(0, 0, 4.5, 0),
                    textAlign: TextAlign.center,
                    style: XStyleUtils.getTextStyle(
                      fontSize: textSize,
                      color: textColor,
                      //lineHeight: 1.1,//用苹方字体不用设置
                    ),
                  ),
                  XImageUtils.getImageWidgetWithAsset(
                    'x_select_time_down.png',
                    width: isBigText?14.5:12.5,
                    height: isBigText?14.5:12.5,
                    color: textColor,
                  ),
                ]
            )
        ),
        onTap: (){
          //弹起底部时间选择框控件
          showSelectTimeWidgetByBottomSheet(
            context,
            selectTime: selectTime,
            isLimitSelectTimeOver: isLimitSelectTimeOver,
            isClickMenu2AutoSubmit:isClickMenu2AutoSubmit,
            resultCallback: selectTimeResultCallback,
          );
        }
    );

    bool isCanClickRight=true;
    if(isLimitSelectTimeOver){
      //不能超出当前时间范围
      if(selectTime==null){
        isCanClickRight=false;
      }else{
        DateTime now=XDateUtils.getDateTimeWithString('${DateTime.now().year}-${XNumberUtils.number2StringFillZero(DateTime.now().month)}-${XNumberUtils.number2StringFillZero(DateTime.now().day)}')!;

        if(selectTime.type=='日'){
          DateTime end=XDateUtils.getDateTimeWithString('${selectTime.year}-${XNumberUtils.number2StringFillZero(selectTime.month)}-${XNumberUtils.number2StringFillZero(selectTime.day)}')!;
          if(end.compareTo(now)>=0){
            isCanClickRight=false;
          }
        }else if(selectTime.type=='周'){
          DateTime end=XDateUtils.getDateTimeWithString(XDateUtils.getStringTimeFormat(dateTime: XDateUtils.getYearWeekRanges(selectTime.year!, selectTime.yearWeek!)['end'], format: XDateUtils.formatTimeDefault3))!;
          if(end.compareTo(now)>=0){
            isCanClickRight=false;
          }
        }else if(selectTime.type=='月'){
          int maxDay=XDateUtils.getDaysOfMonth(selectTime.year!, selectTime.month!);
          DateTime end=XDateUtils.getDateTimeWithString('${selectTime.year}-${XNumberUtils.number2StringFillZero(selectTime.month)}-$maxDay')!;
          if(end.compareTo(now)>=0){
            isCanClickRight=false;
          }
        }else if(selectTime.type=='季度' || selectTime.type=='季'){
          DateTime end=XDateUtils.getDateTimeWithString(XDateUtils.getStringTimeFormat(dateTime: XDateUtils.getQuarterRange(selectTime.year!, selectTime.quarter!)['end'], format: XDateUtils.formatTimeDefault3))!;
          if(end.compareTo(now)>=0){
            isCanClickRight=false;
          }
        }else if(selectTime.type=='自定义'){
        }
      }
    }


    Widget changeSelectDateLeftWidget =XWidgetUtils.getWidgetGestureDetector(
        child: XWidgetUtils.getLayoutContainer(
            backgroundColor: backgroundColor,
            alignment: Alignment.center,
            padding: XWidgetUtils.getEdgeInsets(50, 0, 10, 0),
            height: totalHeight,
            child: XImageUtils.getImageWidgetWithAsset(
              'x_select_time_change_left.png',
              width: isBigText?22.5:16.5,
              height: isBigText?22:16,
              color: textColor,
            )
        ),
        onTap: (){
          if(onClickOfLeftButton!=null && selectTime!=null){
            onClickOfLeftButton(XSelectTimeUtils.preDate(selectTime));
          }

        }
    );

    Widget changeSelectDateRightWidget =XWidgetUtils.getWidgetGestureDetector(
        child: XWidgetUtils.getLayoutContainer(
            backgroundColor: backgroundColor,
            alignment: Alignment.center,
            padding: XWidgetUtils.getEdgeInsets(5, 0, 50, 0),
            height: totalHeight,
            child: XImageUtils.getImageWidgetWithAsset(
              'x_select_time_change_right.png',
              width: isBigText?22.5:16.5,
              height: isBigText?22:16,
              color: isCanClickRight?textColor:const Color(0xFFCCCCCC),
            )
        ),
        onTap: (){
          if(onClickOfRightButton!=null && selectTime!=null && isCanClickRight){
            onClickOfRightButton(XSelectTimeUtils.nextDate(selectTime));
          }

        }
    );

    if(isShowLeftRightChangeButton && selectTime!=null){

      String? type=selectTime.type;
      if('日'==type||'月'==type||'周'==type||'季度'==type||'季'==type){
        listWidgets.add(changeSelectDateLeftWidget);
        listWidgets.add(selectDateWidget);
        listWidgets.add(changeSelectDateRightWidget);
      }else if('自定义'==type){
        listWidgets.add(selectDateWidget);
      }
    }else{
      listWidgets.add(selectDateWidget);
    }

    return XWidgetUtils.getLayoutContainer(
        height: totalHeight,
        alignment: Alignment.center,
        backgroundColor: backgroundColor,
        child: XWidgetUtils.getLayoutRow(
            mainAxisSize:MainAxisSize.min,
            mainAxisAlignment:MainAxisAlignment.center,
            children: listWidgets
        )
    );

  }


  /////////////////////////////////////////////////////////

  //2.弹出选择时间控件(底部弹框)
  static void showSelectTimeWidgetByBottomSheet(
      BuildContext contextOfScaffold,
      {
        required XFunctionResultCallback<dynamic> resultCallback,
        XBeanSelectTime? selectTime,
        bool isLimitSelectTimeOver=false,
        bool isClickMenu2AutoSubmit=true,
      }
      ){

    XFlutterUtils.openBottomSheet(
      context: contextOfScaffold,
      backgroundColor: Colors.white,
      shape: const RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
            topLeft: Radius.circular(15),
            topRight: Radius.circular(15),
          )
      ),
      onCallbackResult: resultCallback,
      enableDrag: false,
      widgetState: XSelectTimeState(
          selectTime:selectTime??XBeanSelectTime(),
          isLimitSelectTimeOver:isLimitSelectTimeOver,
          isClickMenu2AutoSubmit: isClickMenu2AutoSubmit
      ),
    );
  }

  //上一个日期
  static XBeanSelectTime preDate(XBeanSelectTime selectTime){
    String type=selectTime.type!;

    DateTime currentDateTime=XDateUtils.getDateTimeWithString('${selectTime.year}-${XNumberUtils.number2StringFillZero(selectTime.month)}-${XNumberUtils.number2StringFillZero(selectTime.day)}')!;


    if('日'==type){

      DateTime preDay=currentDateTime.subtract(const Duration(days: 1));
      selectTime.year=preDay.year;
      selectTime.month=preDay.month;
      selectTime.day=preDay.day;
      //
      selectTime.yearWeek=XDateUtils.getYearWeekNumber(date:preDay)['yearWeek'];
      selectTime.quarter=XDateUtils.getQuarterNumber(date: preDay);
      //
      selectTime.customStartTime=XDateUtils.getStringTimeFormat(dateTime:preDay,format:XDateUtils.formatTimeDefault3);
      selectTime.customEndTime=XDateUtils.getStringTimeFormat(dateTime:preDay,format:XDateUtils.formatTimeDefault3);

    }else if('周'==type){

      DateTime preWeek=currentDateTime.subtract(const Duration(days: 7));
      selectTime.year=preWeek.year;
      selectTime.month=preWeek.month;
      selectTime.day=preWeek.day;
      //
      selectTime.yearWeek=XDateUtils.getYearWeekNumber(date:preWeek)['yearWeek'];
      selectTime.quarter=XDateUtils.getQuarterNumber(date: preWeek);
      //
      Map<String,DateTime> range=XDateUtils.getYearWeekRanges(selectTime.year!, selectTime.yearWeek!);
      DateTime? start=range['start'];
      DateTime? end=range['end'];
      selectTime.customStartTime=XDateUtils.getStringTimeFormat(dateTime:start,format:XDateUtils.formatTimeDefault3);
      selectTime.customEndTime=XDateUtils.getStringTimeFormat(dateTime:end,format:XDateUtils.formatTimeDefault3);

    }else if('月'==type){

      if(currentDateTime.month==1){
        DateTime preMonth=DateTime(currentDateTime.year-1, 12, 1);
        selectTime.year=preMonth.year;
        selectTime.month=preMonth.month;
        selectTime.day=1;

      }else{
        DateTime preMonth=DateTime(currentDateTime.year, currentDateTime.month - 1, 1);
        selectTime.year=preMonth.year;
        selectTime.month=preMonth.month;
        selectTime.day=1;
      }

      //
      DateTime newDate=DateTime(selectTime.year!,selectTime.month!,selectTime.day!);
      selectTime.yearWeek=XDateUtils.getYearWeekNumber(date:newDate)['yearWeek'];
      selectTime.quarter=XDateUtils.getQuarterNumber(date: newDate);
      //
      DateTime? start=DateTime(selectTime.year!,selectTime.month!,1);
      DateTime? end=DateTime(selectTime.year!,selectTime.month!,XDateUtils.getDaysOfMonth(selectTime.year!,selectTime.month!));
      selectTime.customStartTime=XDateUtils.getStringTimeFormat(dateTime:start,format:XDateUtils.formatTimeDefault3);
      selectTime.customEndTime=XDateUtils.getStringTimeFormat(dateTime:end,format:XDateUtils.formatTimeDefault3);

    }else if('季'==type||'季度'==type){

      if(selectTime.quarter==1){
        selectTime.quarter=4;
        selectTime.year=selectTime.year!-1;

        DateTime? start=XDateUtils.getQuarterRange(selectTime.year!,selectTime.quarter!)['start'];
        selectTime.month=start?.month;
        selectTime.day=start?.day;
        selectTime.yearWeek=XDateUtils.getYearWeekNumber(date:start)['yearWeek'];
      }else{
        selectTime.quarter=selectTime.quarter!-1;
        DateTime? start=XDateUtils.getQuarterRange(selectTime.year!,selectTime.quarter!)['start'];
        selectTime.month=start?.month;
        selectTime.day=start?.day;
        selectTime.yearWeek=XDateUtils.getYearWeekNumber(date:start)['yearWeek'];
      }
      //
      Map<String,DateTime> range=XDateUtils.getQuarterRange(selectTime.year!, selectTime.quarter!);
      DateTime? start=range['start'];
      DateTime? end=range['end'];
      selectTime.customStartTime=XDateUtils.getStringTimeFormat(dateTime:start,format:XDateUtils.formatTimeDefault3);
      selectTime.customEndTime=XDateUtils.getStringTimeFormat(dateTime:end,format:XDateUtils.formatTimeDefault3);

    }

    //当前时分秒
    selectTime.hourMinSec=XDateUtils.getCurrentTime_String(format: XDateUtils.formatTimeDefault6);

    return selectTime;
  }

  //下一个日期
  static XBeanSelectTime nextDate(XBeanSelectTime selectTime){

    String type=selectTime.type!;

    DateTime currentDateTime=XDateUtils.getDateTimeWithString('${selectTime.year}-${XNumberUtils.number2StringFillZero(selectTime.month)}-${XNumberUtils.number2StringFillZero(selectTime.day)}')!;


    if('日'==type){

      DateTime nextDay=currentDateTime.add(const Duration(days: 1));
      selectTime.year=nextDay.year;
      selectTime.month=nextDay.month;
      selectTime.day=nextDay.day;
      //
      selectTime.yearWeek=XDateUtils.getYearWeekNumber(date:nextDay)['yearWeek'];
      selectTime.quarter=XDateUtils.getQuarterNumber(date: nextDay);
      //
      selectTime.customStartTime=XDateUtils.getStringTimeFormat(dateTime:nextDay,format:XDateUtils.formatTimeDefault3);
      selectTime.customEndTime=XDateUtils.getStringTimeFormat(dateTime:nextDay,format:XDateUtils.formatTimeDefault3);

    }else if('周'==type){

      DateTime nextWeek=currentDateTime.add(const Duration(days: 7));
      selectTime.year=nextWeek.year;
      selectTime.month=nextWeek.month;
      selectTime.day=nextWeek.day;
      //
      selectTime.yearWeek=XDateUtils.getYearWeekNumber(date:nextWeek)['yearWeek'];
      selectTime.quarter=XDateUtils.getQuarterNumber(date: nextWeek);
      //
      Map<String,DateTime> range=XDateUtils.getYearWeekRanges(selectTime.year!, selectTime.yearWeek!);
      DateTime? start=range['start'];
      DateTime? end=range['end'];
      selectTime.customStartTime=XDateUtils.getStringTimeFormat(dateTime:start,format:XDateUtils.formatTimeDefault3);
      selectTime.customEndTime=XDateUtils.getStringTimeFormat(dateTime:end,format:XDateUtils.formatTimeDefault3);

    }else if('月'==type){

      if(currentDateTime.month==12){
        DateTime nextMonth=DateTime(currentDateTime.year+1, 1, 1);
        selectTime.year=nextMonth.year;
        selectTime.month=nextMonth.month;
        selectTime.day=1;
      }else{
        DateTime nextMonth=DateTime(currentDateTime.year, currentDateTime.month + 1, 1);
        selectTime.year=nextMonth.year;
        selectTime.month=nextMonth.month;
        selectTime.day=1;
      }

      //
      DateTime newDate=DateTime(selectTime.year!,selectTime.month!,selectTime.day!);
      selectTime.yearWeek=XDateUtils.getYearWeekNumber(date:newDate)['yearWeek'];
      selectTime.quarter=XDateUtils.getQuarterNumber(date: newDate);
      //
      DateTime? start=DateTime(selectTime.year!,selectTime.month!,1);
      DateTime? end=DateTime(selectTime.year!,selectTime.month!,XDateUtils.getDaysOfMonth(selectTime.year!,selectTime.month!));
      selectTime.customStartTime=XDateUtils.getStringTimeFormat(dateTime:start,format:XDateUtils.formatTimeDefault3);
      selectTime.customEndTime=XDateUtils.getStringTimeFormat(dateTime:end,format:XDateUtils.formatTimeDefault3);

    }else if('季'==type||'季度'==type){

      if(selectTime.quarter==4){
        selectTime.quarter=1;
        selectTime.year=selectTime.year!+1;

        DateTime? start=XDateUtils.getQuarterRange(selectTime.year!,selectTime.quarter!)['start'];
        selectTime.month=start?.month;
        selectTime.day=start?.day;
        selectTime.yearWeek=XDateUtils.getYearWeekNumber(date:start)['yearWeek'];
      }else{
        selectTime.quarter=selectTime.quarter!+1;
        DateTime? start=XDateUtils.getQuarterRange(selectTime.year!,selectTime.quarter!)['start'];
        selectTime.month=start?.month;
        selectTime.day=start?.day;
        selectTime.yearWeek=XDateUtils.getYearWeekNumber(date:start)['yearWeek'];
      }
      //
      Map<String,DateTime> range=XDateUtils.getQuarterRange(selectTime.year!, selectTime.quarter!);
      DateTime? start=range['start'];
      DateTime? end=range['end'];
      selectTime.customStartTime=XDateUtils.getStringTimeFormat(dateTime:start,format:XDateUtils.formatTimeDefault3);
      selectTime.customEndTime=XDateUtils.getStringTimeFormat(dateTime:end,format:XDateUtils.formatTimeDefault3);

    }

    //当前时分秒
    selectTime.hourMinSec=XDateUtils.getCurrentTime_String(format: XDateUtils.formatTimeDefault6);

    return selectTime;

  }


  //显示选择的时间字符串
  static String showSelectTimeString({required XBeanSelectTime? selectTime}){
    String showTimeStr='';
    if(selectTime!=null){
      String? type=selectTime.type;
      if('日'==type){
        showTimeStr='${selectTime.year}年${XNumberUtils.number2StringFillZero(selectTime.month)}月${XNumberUtils.number2StringFillZero(selectTime.day)}日';
      }else if('月'==type){
        showTimeStr='${selectTime.year}年${XNumberUtils.number2StringFillZero(selectTime.month)}月';
      }else if('周'==type){
        showTimeStr='${selectTime.year}年第${selectTime.yearWeek}周';
      }else if('季度'==type||'季'==type){
        showTimeStr='${selectTime.year}年第${selectTime.quarter}季度';
      }else if('自定义'==type){
        showTimeStr='${selectTime.customStartTime} ~ ${selectTime.customEndTime}';
      }
    }

    return showTimeStr;
  }



}
